package com.tdxy.am2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;

public class MyPanel extends JPanel implements Runnable{
    private int x ;
    private int y ;

    private boolean isCreate ; // 是否可以创建子弹
    List list ;

    public MyPanel (){
        list = new ArrayList();
        x = 0 ;
        y = 100 ;
        this.setBounds(0,0,800,600);

        this.addMouseMotionListener(new MouseMotionAdapter() {
            // 实现拖动鼠标的方法
            public void mouseDragged(MouseEvent e) {
                int y = e.getY();
                if ( y < 600 && y > 435 ){
                    x = e.getX() - 35 ;
                    repaint();
                }
            }
        });

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
//                System.out.println("鼠标按下了");
                isCreate = true ;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
//                System.out.println("鼠标抬起了");
                isCreate = false ;
            }
        });

    }

    // 此时,如果有两个方法, 想对同一个值进行改变。要将值提取成变量。
    @Override
    public void paint(Graphics g) {
        super.paint(g); // 保留当前行
        g.setColor(Color.BLACK);
        g.fillRect(0,0,800,600);

        Image image = new ImageIcon("D:\\tdxy\\workspace\\demo07.01\\demo07.04\\src\\com\\tdxy\\am2\\img\\hero1.png").getImage();
        g.drawImage(image,x,470,70,100,null);

        if(list.size()>0){
            g.setColor(Color.GREEN);
            for (int i = 0; i < list.size(); i++) {
                Circle c = (Circle) list.get(i);
                g.fillOval(c.getX(),c.getY(),c.getWidth(),c.getHeight());
            }
        }

//        g.setColor(Color.GREEN);
//        g.fillRect(x,535,150,30);

//        g.fillOval(100,y,100,100);

    }

    // 将 Runnable 接口的 run () 进行重写。
    public void run(){

        while(true){
            try {
                Thread.sleep(50);  // 控制绘画的速率
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 实现创建子弹
            if(isCreate){
                Circle c = new Circle(x,470,20,20);
                if(list.size()<10){
                    list.add(c);
                }
            }
            // 子弹向上移动
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    Circle c = (Circle) list.get(i);
                    c.setY(c.getY() - 15);
                }
                // 超出屏幕的子弹进行消除。
                for (int i = 0; i < list.size(); i++) {
                    Circle c = (Circle) list.get(i);
                    if(c.getY() < 0 ){
                        list.remove(c);
//                        list.remove(i);
                    }
                }
            }


            /*y = y+3 ;  // 控制圆下移的速度.
            if (y > 580) {
                y = 0 ;
            }*/
            repaint(); // 重新绘画
        }


    }
}
